package com.hackordie.commons.hardware.components;

import java.util.ArrayList;
import java.util.List;

import com.hackordie.commons.software.AProgram;
import com.hackordie.commons.software.TextFile;
import com.hackordie.commons.software.applications.FirewallDesencryptor;
import com.hackordie.constants.EComponentsType;
import com.hackordie.constants.ESoftwareType;

public class HardDisk extends AComponent {

    int totalCapacity;
    int usedCapacity;
    List<AProgram> files = new ArrayList<AProgram>();

    public HardDisk(String componentName, int energyToTurOn, int totalCapacity) {
        super(componentName, EComponentsType.HardDisk, energyToTurOn);

        this.totalCapacity = totalCapacity;
        this.usedCapacity = 0;
    }

    public int getUsedCapacity() {
        return usedCapacity;
    }

    public void setUsedCapacity(int usedCapacity) {
        this.usedCapacity = usedCapacity;
    }

    public int getTotalCapacity() {
        return totalCapacity;
    }
    
    public int getAvailableCapacity() {
        return totalCapacity - usedCapacity;
    }

    public AProgram getFileByName(String name) {
        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getName().equals(name)) {
                return files.get(i);
            }
        }
        return null;
    }

    public List<AProgram> getFilesByType(ESoftwareType type) {
        List<AProgram> filesToReturn = new ArrayList<AProgram>();

        for (int i = 0; i < files.size(); i++) {
            if (files.get(i).getSoftwareType().equals(type)) {
                filesToReturn.add(files.get(i));
            }
        }

        return filesToReturn;
    }

    // functions

    public boolean setFile(AProgram file) {
        if (!started) {
            return false;
        }

        AProgram newFile = createNewFile(file);
        
        if (newFile != null && newFile.getSize() <= (totalCapacity - usedCapacity)) {
            files.add(newFile);
            usedCapacity += file.getSize();
            return true;
        }
        return false;
    }

    public void deleteFile(String name) {
        if (started) {
            for (int i = 0; i < files.size(); i++) {
                if (files.get(i).getName().equals(name)) {
                    usedCapacity -= files.get(i).getSize();
                    files.remove(i);
                }
            }
        }
    }

    private AProgram createNewFile(AProgram file) {
        
        switch(file.getSoftwareType()) {
        case textFile:
            return new TextFile(file.getName(), 
                    file.getDescription(), 
                    file.getSize(), 
                    file.getRequiredRam(), 
                    file.getRequiredProcessor());
            
        case application:
            if(file instanceof FirewallDesencryptor) {
            return new FirewallDesencryptor(file.getName(), 
                    file.getDescription(), 
                    file.getSize(), 
                    file.getRequiredRam(), 
                    file.getRequiredProcessor());
            }
//            } else {
//                return new AApplication(file.getName(), 
//                        file.getDescription(), 
//                        file.getSize(), 
//                        file.getRequiredRam(), 
//                        file.getRequiredProcessor());
            }
        
     
        return null; //TODO de momento solo hay archivos de texto y applications, agregar aqui para que
                     //cada disco duro tenga una copia distinta de cada archivo
            
    }
}
